var revenueApi = require('../../script/revenue.js');
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    DETAIL_ID: '',
    AssistPeopleList: [],
    PSysUser: [],
    ShowPSysUser: [],
    ShowPop: false,
    searchText: '',
    activeIndex: -1,
    noData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPSysUser();
    if (options) {
      this.setData({
        DETAIL_ID: options.id
      });
      this.getAssistPeople();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getAssistPeople() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    const params = {
      Data: JSON.stringify({
        Detail_Id: this.data.DETAIL_ID
      })
    }
    revenueApi.ViewAssistPeople(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        const list = res.data.AssistPeopleList.length == 0 ? [{
          UserNo: 'none',
          UserName: '请选择协查员'
        }] : res.data.AssistPeopleList
        this.setData({
          AssistPeopleList: list
        });
      }
    })
  },
  getPSysUser() {
    revenueApi.PSysUserListPost().then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        this.setData({
          PSysUser: res.data,
          ShowPSysUser: res.data
        });
      }
    })
  },
  addEmpty() {
    const AssistPeopleList = [...this.data.AssistPeopleList];
    if (AssistPeopleList.length > 0) {
      const last = AssistPeopleList[AssistPeopleList.length - 1];
      if (last.UserNo === 'none') {
        wx.showToast({
          title: '请补充完整数据后添加',
          icon: 'none',
          duration: 1500
        });
        return;
      }
    }
    AssistPeopleList.push({
      UserNo: 'none',
      UserName: '请选择协查员'
    });
    this.setData({
      AssistPeopleList,
      noData: false
    });
  },
  deleteUser(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    const AssistPeopleList = [...this.data.AssistPeopleList];
    AssistPeopleList.splice(index, 1);
    this.setData({
      AssistPeopleList,
      noData: AssistPeopleList.length == 0
    });
  },
  openPop(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      ShowPop: true,
      activeIndex: index
    });
  },
  searchChange(e) {
    let val = e.detail.value;
    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      const list = [...this.data.PSysUser];
      const newList = list.filter(item => {
        return item.USER_NAME.indexOf(val) > -1
      })
      this.setData({
        searchText: val,
        ShowPSysUser: newList
      });
    })
  },
  leave() {
    this.setData({
      ShowPSysUser: [...this.data.PSysUser]
    });
  },
  addUser(e) {
    var info = e.currentTarget.dataset.info;
    const list = [...this.data.AssistPeopleList];
    list[this.data.activeIndex] = {
      UserNo: info.USER_NO,
      UserName: info.USER_NAME
    };
    this.setData({
      AssistPeopleList: list,
      ShowPop: false,
      searchText: '',
      activeIndex: -1
    });
  },
  AddAssistPeople() {
    const last = this.data.AssistPeopleList[this.data.AssistPeopleList.length - 1];
    if (last.UserNo === 'none') {
      wx.showToast({
        title: '请补充完整数据后提交',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      title: '添加中...',
      mask: true
    });
    const params = {
      Data: JSON.stringify({
        Detail_Id: this.data.DETAIL_ID,
        assistPeopleList: this.data.AssistPeopleList,
      }),
    }
    revenueApi.AddAssistPeople(params).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: res.info,
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.info,
          icon: 'error',
          duration: 2000
        })
      }
    }).catch(() => {
      wx.hideLoading();
    })
  }
})